﻿using SqlMapper.SqlElements;
using System.Xml;

namespace SqlMapper.Serializers
{
    public sealed class SelectDeSerializer
    {
        public static Select Deserialize(XmlNode node)
        {
            Select select = new Select();
            var prop = NodeUtils.ParseAttributes(node);

            select.Id = NodeUtils.GetStringAttribute(prop, "id");
            select.Sql = NodeUtils.GetStringAttribute(prop, "sql");
            select.Prepend = NodeUtils.GetStringAttribute(prop, "prepend");

            var factory = new DeSerializerFactory();
            int count = node.ChildNodes.Count;
            for (int i = 0; i < count; i++)
            {
                var name = node.ChildNodes[i].LocalName;
                IDeSerializer deSerializer = factory.GetDeSerializer(name);
                if (deSerializer != null)
                    select.Statements.Add(deSerializer.Deserialize(node.ChildNodes[i]));
            }

            return select;
        }
    }
}
